#!/usr/bin/env python3
# coding: utf-8
"""
2024-02-18 脚本管理窗口
"""

from PySide6.QtCore import Slot, QSize
from PySide6.QtGui import QAction, QKeySequence
from PySide6.QtWidgets import QMainWindow, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QWidget, QMessageBox, QLineEdit, QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView
from PySide6.QtCore import Qt, QModelIndex
from PySide6.QtGui import QGuiApplication, QCloseEvent
## from PySide6.QtCore.Qt import 

##from PySide6.QtCore import WindowState

## from top.wardyan.core.utils import winformutils
from top.wardyan.core.action.baseaction import WMainWindow
from top.wardyan.core.vo.vos import *
from top.wardyan.core.widges.tables import WTableView

##from warningmain import *
from top.wardyan.core.wdb.core.DBPool import POOL

from top.wardyan.core.vo.vos import *
from top.wardyan.core.wdb.core.db import *
from top.wardyan.projects.warning.warningedit import WWarningEdit

## cn = rpyc.connect("127.0.0.1", 8899)
# print("time is {}".format(cn.root.get_time()))
# print(cn.root.script_list())
import sys


## print(sys.argv[0])



def toobject(jsonstr) :

    return 


class WWarningWindow (QWidget) :
    # def __init__(self, parent: Optional[PySide6.QtWidgets.QWidget] = ..., f: PySide6.QtCore.Qt.WindowType = ...) -> None:
    #     super().__init__(parent, f)


    def __init__(self) -> None:
        ##global cn
        super().__init__()
        self.table : WTableView = None
        self.main()
        self.refreshTable()

    
        
        
    def handle_double_click(self, index: QModelIndex) -> None:
        """
        处理单元格的双击事件。
        :param index: 被双击的单元格的模型索引。
        """
        row = index.row()
        column = index.column()
        data : Record = self.table.get_row_data(row)
        print(f"Double click detected at Row: {row}, Column: {column}", data)
        
        if data is not None :
            warningID = data.getInt("sID")
            win : WWarningEdit = WWarningEdit(warningID)
            rs = win.exec()
            # field = data.get("field")
            # ## win = TableColEditor("tablemanagercoledit", self.tablename, field)
            # win = TableColEditor(self.widgetname, self.tablename, field)
            # # win.exec()
            # rs : int = win.exec()
            # if rs is not None and rs > 0 :
            #     self.refreshTable()

   

    def main(self) :
        HEIGHT = 30
        layout : QVBoxLayout = QVBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignTop)
        self.setLayout(layout)
        

        ## 按钮: 搜索 脚本清单 新增脚本 表/视图结构
        # btn : QPushButton = QPushButton("TESTING")
        # btn.setMinimumHeight(50)
        # ## btn.clicked.connect(button_click_script)
        # layout.addWidget(btn)
        ctrlw : QWidget = QWidget()
        ctrllayout : QHBoxLayout = QHBoxLayout()
        ctrlw.setLayout(ctrllayout)
        layout.addWidget(ctrlw)


        ctrllayout.setAlignment(Qt.AlignmentFlag.AlignRight)
        

        ctrlt : QWidget = QWidget()
        tbllayout : QHBoxLayout = QHBoxLayout()
        ctrlt.setLayout(tbllayout)
        layout.addWidget(ctrlt)

        self.table = WTableView(self, "warnwindow", "maintable", dbclick = self.handle_double_click)
        tbllayout.addWidget(self.table)

    def refreshTable(self) :

        data : list[Record] = []
        # r : Record = Record()
        # r.set("name", "n1")
        # r.set("age", "2")
        # r.set("school", "??")
        # data.append(r)
        # data.append(r)
        # data.append(r)
        # data.append(r)
        # data.append(r)
        # ## self.renderTable(data)

        rs = query("select * from dbo.st_warning as w (nolock)", ())
        if rs is None :
            rs = []
        data = listtorecords(rs)
        ## print(rs)
        

        self.table.render(data)
        
        

# if __name__ == "__main__" :     ## or 1 == 1
#     app = QApplication([])
#     w = WWarningEditor()
#     ## w = WScriptWindow()
#     w.show()
#     app.exec()